package net.tv90.bfmccore.listener;

import net.tv90.bfmccore.SlainListener;
import net.tv90.bfmccore.constant.RewardConstant;
import net.tv90.bfmccore.event.RibbonGetEvent;
import net.tv90.bfmccore.event.RibbonUnlockEvent;
import net.tv90.bfmccore.util.HUD.HUD;
import net.tv90.bfmccore.util.HUD.HUDDelayedQueue;
import net.tv90.bfmccore.util.HUD.Text;
import net.tv90.bfmccore.util.vector.Vector2i;
import org.bitterorange.flansmodslain.actions.Money;
import org.bitterorange.flansmodslain.actions.Point;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Tag;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import static net.tv90.bfmccore.BFMCCore.PLUGIN_ROOT;
import static net.tv90.bfmccore.SlainListener.RibbonCacheMap;
import static net.tv90.bfmccore.SlainListener.RibbonNumMap;
import static net.tv90.bfmccore.helper.FactionHelper.getFaction_Lang;
import static net.tv90.bfmccore.util.file.FileUtils.writeAllText;

public class RibbonListener implements Listener {

    HashMap<String, Integer> medal = new HashMap<>();

    public static void printRibbonData() throws Exception {
        File dataDirectory = new File(PLUGIN_ROOT, "ribbondata");
        File file = new File(dataDirectory, "ribbondata.yml");
        writeAllText(file, (new Yaml()).dumpAs(RibbonNumMap, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
    }

    /**
     * 清除勋带缓存数据，用于战局结束
     */
    public static void clearRibbonData() {
        RibbonCacheMap.clear();
    }

    @EventHandler
    public void onRibbonGet(RibbonGetEvent e) throws Exception {
        Player player = e.getPlayer();
        String ribbonType = e.getRibbonType();
        String weaponType = e.getWeaponType();
        int ribbonNum = e.getRibbonNum();
        Bukkit.broadcastMessage("§e[管理员]玩家" + player.getName() + "(" + getFaction_Lang(player) + ")获得了" + weaponType + "勋带");

        printRibbonData();
        Bukkit.getServer().getPluginManager().callEvent(new RibbonUnlockEvent(ribbonType, ribbonNum, player, SlainListener.RibbonNumMap));

        int point;
        int money;
        if (Objects.equals(ribbonType, "Ribbon_SquadDeploy")
                || Objects.equals(ribbonType, "Ribbon_Ammo")
                || Objects.equals(ribbonType, "Ribbon_Heal")
                || Objects.equals(ribbonType, "Ribbon_Revive")) {
            point = (int) (20 * RewardConstant.POINT_MAGNIFICATION);
            money = (int) (40 * RewardConstant.MONEY_MAGNIFICATION);
        } else if (Objects.equals(ribbonType, "Ribbon_ME")
                || Objects.equals(ribbonType, "Ribbon_GunMaster")
                || Objects.equals(ribbonType, "Ribbon_Farshoot")) {
            point = (int) (30 * RewardConstant.POINT_MAGNIFICATION);
            money = (int) (60 * RewardConstant.MONEY_MAGNIFICATION);
        } else {
            point = (int) (10 * RewardConstant.POINT_MAGNIFICATION);
            money = (int) (20 * RewardConstant.MONEY_MAGNIFICATION);
        }
        Point.add(player, point);
        Money.give(player, money);
        player.sendMessage("§e[管理员]你获得" + point + "军衔积分和" + money + "游戏币的奖励");

        Text text = new Text(new Vector2i(-1, 100), weaponType + "勋带", 1.5D);
        Text text2 = new Text(new Vector2i(-1, 30), String.valueOf(10 * ribbonNum), 1.5D);
        List<Text> list = new ArrayList<>();
        list.add(text);
        list.add(text2);
        HUD hud = new HUD(player, "[local]Ribbons/" + ribbonType + ".png", "custom.award", list,
                new Vector2i(-1, 10), new Vector2i(130, 130), 4);
        HUDDelayedQueue.addHUD(player, hud);
    }


}
